package com.hnhcyy.goods.pojo;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.hnhcyy.b2b.common.util.Arith;
import com.hnhcyy.b2b.common.util.Property;
import com.hnhcyy.goods.properties.ImageConfig;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

//import com.hnhcyy.b2b.promotion.pojo.Give;

/**
 * 商品档案
 */
@Document(indexName = "eshop", type = "goods", createIndex = false)
public class Goods implements Serializable{

	private static final long serialVersionUID = -8543018463792459471L;
	/**
	 * 商品ID
	 */
	@Property(value = "FNumber")
	@Id
	private String spid;
	/**
	 * 版本号（图片）
	 */
	@JsonIgnore
	private Integer fver;
	/**
	 * 金蝶ID
	 */
	@Property(value = "FMATERIALID")
	@JsonIgnore
	private Integer kid;	
	/**
	 * 商品编号
	 */
	@JsonInclude(Include.NON_NULL)
	private String spbh;
	/**
	 * 商品名称
	 */
	@Property(value = "FName")
	@JsonInclude(Include.NON_NULL)
	@Field(store = true)
	private String spmch;	
	/**
	 * 助记码
	 */
	@Property(value = "FMnemonicCode")
	@JsonIgnore
	private String zjm;
	/**
	 * 生产厂家
	 */
	@Property(value = "Factory")
	@JsonInclude(Include.NON_NULL)
	private String shpchd;
	/**
	 * 商品规格
	 */
	@Property(value = "FSpecification")
	@JsonInclude(Include.NON_NULL)
	private String shpgg;
	/**
	 * 批准文号
	 */
	@Property(value = "FApprovalNumber")
	@JsonInclude(Include.NON_NULL)
	private String pizhwh;
	/**
	 * 类别
	 */
	@Property(value = "FCertificationSpace")
	@JsonInclude(Include.NON_NULL)
	private String leibie;
	/**
	 * 单位
	 */
	@Property(value = "FBaseUnitId")
	@JsonInclude(Include.NON_NULL)
	private String dw;
	/**
	 * 剂型
	 */
	@Property(value = "FDOSAGE")
	@JsonInclude(Include.NON_NULL)
	private String jixing;
	/**
	 * 通用名
	 */
	@Property(value = "FAbbreviation")
	@JsonInclude(Include.NON_NULL)
	private String tongym;
	/**
	 * 件装量
	 */
	@Property(value = "FPieceQty")
	@JsonInclude(Include.NON_NULL)
	private Integer jlgg;
	/**
	 * 中包量
	 */
	@JsonInclude(Include.NON_NULL)
	private Integer zhongbz;	
	/**
	 * 起订量
	 */
	@Property(value = "FMINSHL")
	@JsonInclude(Include.NON_NULL)
	private Integer minshl;	
	/**
	 * 倍增量
	 */
	@Property(value = "FZHONGBZ")
	@JsonInclude(Include.NON_NULL)
	private Integer zbaozshl;
	/**
	 * 处方分类
	 */
	@Property(value = "FIsOTC")
	@JsonInclude(Include.NON_NULL)
	private String chuffl;
	/**
	 * 存储条件
	 */
	@Property(value = "FStockCondition")
	@JsonInclude(Include.NON_NULL)
	private String cunchtj;
	/**
	 * 是否电子监管
	 */
	@Property(value = "FElectCodeManag")
	@JsonInclude(Include.NON_NULL)
	private String isDzjg;	
	/**
	 * 商品条码
	 */
	@Property(value = "FBARCODE")
	@JsonInclude(Include.NON_NULL)
	private String sptm;
	/**
	 * 单品长
	 */
	@Property(value = "FLENGTH")
	@JsonIgnore
	private Double itemlength;
	/**
	 *  单品宽
	 */
	@Property(value = "FWIDTH")
	@JsonIgnore
	private Double itemwidth;

	/**
	 *  单品高
	 */
	@Property(value = "FHEIGHT")
	@JsonIgnore
	private Double itemheight;
	/**
	 *  图片
	 */
	@JsonInclude(Include.NON_NULL)
	private String picture;
	/**
	 * 抱团价
	 */	
	@JsonInclude(Include.NON_NULL)
	private Double price;
	/**
	 * 拆零价 
	 */
	@JsonInclude(Include.NON_NULL)
	private Double priceChl;
	/**
	 *  折后价
	 */
	@JsonInclude(Include.NON_NULL)
	private Double priceDiscount;
	/**
	 *  零售价
	 */
	@JsonInclude(Include.NON_NULL)
	private Double lingshoujia;		
	/**
	 * 类别id
	 */
	@JsonInclude(Include.NON_NULL)
	private Integer flbid;	
	/**
	 *  类别2
	 */
	@JsonInclude(Include.NON_NULL)
	private String leibie2;	
	/**
	 *  销售占比
	 */
	@JsonInclude(Include.NON_NULL)
	private Double xlzb;
	/**
	 *  一句话卖点1
	 */
	@JsonInclude(Include.NON_NULL)
	private String sellPoint1;	
	/**
	 *  一句话卖点1
	 */
	@JsonInclude(Include.NON_NULL)
	private String sellPoint2;
	/**
	 *  资质
	 */
	@JsonInclude(Include.NON_NULL)
	private Boolean zizhi;
	
	/**
	 *  是否抱团品种
	 */
	@Property(value = "FISBTPZ")
	@JsonInclude(Include.NON_NULL)
	private String isBtpz;	
	/**
	 *  是否重点品种
	 */
	@Property(value = "FISZDPZ")
	@JsonInclude(Include.NON_NULL)
	private String isZdpz;	
	/**
	 *  是否GSP药品
	 */
	@Property(value = "FIsGSPMaterial")
	@JsonInclude(Include.NON_NULL)
	private Integer isGspMaterial;
	
	/**
	 *  以下内容与数据表不对应
	 */
	@JsonInclude(Include.NON_NULL)
	private GoodsExtend goodsExtend;
	
	/**
	 * 关联库存
	 */
	@JsonInclude(Include.NON_NULL)
	private SpBal spBal;
	
	/**
	 * 关联缺货信息
	 */
	@JsonInclude(Include.NON_NULL)
	private Spkfklack spkfklack;
	
	/**
	 * 促销信息
	 */
	/*@JsonInclude(Include.NON_NULL)
	private Give give;*/
	
	/**
	 * 数量（移动端，用于快速下单）
	 */
	@JsonInclude(Include.NON_NULL)
	private Integer num;
	
	
	public String getSpid() {
		return spid;
	}

	public void setSpid(String spid) {
		this.spid = spid;
	}

	public Integer getFver() {
		return fver;
	}

	public void setFver(Integer fver) {
		this.fver = fver;
	}

	public Integer getKid() {
		return kid;
	}

	public void setKid(Integer kid) {
		this.kid = kid;
	}

	public String getSpbh() {
		return spbh;
	}

	public void setSpbh(String spbh) {
		this.spbh = spbh;
	}

	public String getSpmch() {
		return spmch;
	}

	public void setSpmch(String spmch) {
		this.spmch = spmch;
	}

	public String getZjm() {
		return zjm;
	}

	public void setZjm(String zjm) {
		this.zjm = zjm;
	}

	public String getShpchd() {
		return shpchd;
	}

	public void setShpchd(String shpchd) {
		this.shpchd = shpchd;
	}

	public String getShpgg() {
		return shpgg;
	}

	public void setShpgg(String shpgg) {
		this.shpgg = shpgg;
	}

	public String getPizhwh() {
		return pizhwh;
	}

	public void setPizhwh(String pizhwh) {
		this.pizhwh = pizhwh;
	}

	public String getLeibie() {
		return leibie;
	}

	public void setLeibie(String leibie) {
		this.leibie = leibie;
	}

	public String getDw() {
		return dw;
	}

	public void setDw(String dw) {
		this.dw = dw;
	}

	public String getJixing() {
		return jixing;
	}

	public void setJixing(String jixing) {
		this.jixing = jixing;
	}

	public String getTongym() {
		return tongym;
	}

	public void setTongym(String tongym) {
		this.tongym = tongym;
	}

	public Integer getJlgg() {
		return jlgg;
	}

	public void setJlgg(Integer jlgg) {
		this.jlgg = jlgg;
	}

	public Integer getZhongbz() {
		return zhongbz;
	}

	public void setZhongbz(Integer zhongbz) {
		this.zhongbz = zhongbz;
	}

	public Integer getMinshl() {
		return minshl;
	}

	public void setMinshl(Integer minshl) {
		this.minshl = minshl;
	}

	public Integer getZbaozshl() {
		return zbaozshl;
	}

	public void setZbaozshl(Integer zbaozshl) {
		this.zbaozshl = zbaozshl;
	}

	public String getChuffl() {
		return chuffl;
	}

	public void setChuffl(String chuffl) {
		this.chuffl = chuffl;
	}

	public String getCunchtj() {
		return cunchtj;
	}

	public void setCunchtj(String cunchtj) {
		this.cunchtj = cunchtj;
	}

	public String getIsDzjg() {
		return isDzjg;
	}

	public void setIsDzjg(String isDzjg) {
		this.isDzjg = isDzjg;
	}

	public String getSptm() {
		return sptm;
	}

	public void setSptm(String sptm) {
		this.sptm = sptm;
	}

	public Double getItemlength() {
		return itemlength;
	}

	public void setItemlength(Double itemlength) {
		this.itemlength = itemlength;
	}

	public Double getItemwidth() {
		return itemwidth;
	}

	public void setItemwidth(Double itemwidth) {
		this.itemwidth = itemwidth;
	}

	public Double getItemheight() {
		return itemheight;
	}

	public void setItemheight(Double itemheight) {
		this.itemheight = itemheight;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public Double getPrice() {
		return price == null ? null : Arith.round(price, 2);
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getPriceChl() {
		return priceChl == null ? null : Arith.round(priceChl, 2);
	}

	public void setPriceChl(Double priceChl) {
		this.priceChl = priceChl;
	}

	public Double getPriceDiscount() {
		return priceDiscount == null ? null : Arith.round(priceDiscount, 2);
	}

	public void setPriceDiscount(Double priceDiscount) {
		this.priceDiscount = priceDiscount;
	}

	public Double getLingshoujia() {
		return lingshoujia == null ? null : Arith.round(lingshoujia, 2);
	}

	public void setLingshoujia(Double lingshoujia) {
		this.lingshoujia = lingshoujia;
	}

	public Integer getFlbid() {
		return flbid;
	}

	public void setFlbid(Integer flbid) {
		this.flbid = flbid;
	}

	public String getLeibie2() {
		return leibie2;
	}

	public void setLeibie2(String leibie2) {
		this.leibie2 = leibie2;
	}

	public Double getXlzb() {
		return xlzb;
	}

	public void setXlzb(Double xlzb) {
		this.xlzb = xlzb;
	}

	public String getSellPoint1() {
		return sellPoint1;
	}

	public void setSellPoint1(String sellPoint1) {
		this.sellPoint1 = sellPoint1;
	}

	public String getSellPoint2() {
		return sellPoint2;
	}

	public void setSellPoint2(String sellPoint2) {
		this.sellPoint2 = sellPoint2;
	}
	public Boolean getZizhi() {
		return zizhi;
	}

	public void setZizhi(Boolean zizhi) {
		this.zizhi = zizhi;
	}

	public String getIsBtpz() {
		return isBtpz;
	}

	public void setIsBtpz(String isBtpz) {
		this.isBtpz = isBtpz;
	}

	public String getIsZdpz() {
		return isZdpz;
	}

	public void setIsZdpz(String isZdpz) {
		this.isZdpz = isZdpz;
	}

	public Integer getIsGspMaterial() {
		return isGspMaterial;
	}

	public void setIsGspMaterial(Integer isGspMaterial) {
		this.isGspMaterial = isGspMaterial;
	}

	public SpBal getSpBal() {
		return spBal;
	}

	public void setSpBal(SpBal spBal) {
		this.spBal = spBal;
	}

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	@JsonInclude(Include.NON_NULL)
	private String minPicture;
	
	public void setMinPicture(String minPicture) {
		this.minPicture = minPicture;
	}

	/*
	 * 套餐fid
	 */
	@JsonInclude(Include.NON_NULL)
	protected Integer givefid;	
	public Integer getGivefid() {return givefid;}
	public void setGivefid(Integer givefid) {this.givefid = givefid;}
	
	/**
	 * 移动端小图
	 * @author sheyongjun
	 * @date 2019年1月21日
	 */
	public String getMinPicture() {
		if(spid == null) {
			return null;
		}
		return ImageConfig.goodsPicturePrefix + spid + "/10.jpg?v=" + fver;
	}

	@JsonInclude(Include.NON_NULL)
	private List<String> pictureList;
	
	public void setPictureList(List<String> pictureList) {
		this.pictureList = pictureList;
	}
	/**
	 * 所有图片url
	 * @author sheyongjun
	 * @date 2019年1月16日
	 */
	public List<String> getPictureList()	
	{
		if(this.picture == null) {
			return null;
		}
		try {
			List<String> picList = new ArrayList<String>();
			for(int i = 0, length = Math.min(picture.length(), 10); i < length; i++){
				if(this.picture.substring(i, i+1).equalsIgnoreCase("1")){
					picList.add(ImageConfig.goodsPicturePrefix + spid + "/" + i + ".jpg?v=" + fver);
				}
			}
			return picList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Spkfklack getSpkfklack() {
		return spkfklack;
	}

	public void setSpkfklack(Spkfklack spkfklack) {
		this.spkfklack = spkfklack;
	}

	public GoodsExtend getGoodsExtend() {
		return goodsExtend;
	}

	public void setGoodsExtend(GoodsExtend goodsExtend) {
		this.goodsExtend = goodsExtend;
		if(this.goodsExtend == null) {
			return;
		}
		// 
		if(this.goodsExtend.getGrossProfit() == null || this.goodsExtend.getGrossProfit() == 0) {
			// 使用bigDecimal以确保相减精度问题
			if(lingshoujia != null && lingshoujia > 0 && price != null && price > 0) {
				// 必须加toString()，否则会出现精度问题
				/*BigDecimal bd1 = new BigDecimal(lingshoujia.toString());
				BigDecimal bd2 = new BigDecimal(price.toString());
				BigDecimal grossProfit = bd1.subtract(bd2).setScale(2, BigDecimal.ROUND_HALF_UP);
				this.goodsExtend.setGrossProfit(grossProfit.doubleValue());*/
				double grossProfit = lingshoujia - price;
				this.goodsExtend.setGrossProfit(Arith.round(lingshoujia - price, 2));
				// 计算毛利率
				this.goodsExtend.setGrossMargin(Arith.round(grossProfit / lingshoujia, 2));
			}
		}
		
	}

	/*public Give getGive() {
		return give;
	}

	public void setGive(Give give) {
		this.give = give;
	}*/

	@Override
	public String toString() {
		return "Goods [spid=" + spid + ", fver=" + fver + ", kid=" + kid + ", spbh=" + spbh + ", spmch=" + spmch
				+ ", zjm=" + zjm + ", shpchd=" + shpchd + ", shpgg=" + shpgg + ", pizhwh=" + pizhwh + ", leibie="
				+ leibie + ", dw=" + dw + ", jixing=" + jixing + ", tongym=" + tongym + ", jlgg=" + jlgg + ", zhongbz="
				+ zhongbz + ", minshl=" + minshl + ", zbaozshl=" + zbaozshl + ", chuffl=" + chuffl + ", cunchtj="
				+ cunchtj + ", isDzjg=" + isDzjg + ", sptm=" + sptm + ", itemlength=" + itemlength + ", itemwidth="
				+ itemwidth + ", itemheight=" + itemheight + ", picture=" + picture + ", price=" + price + ", priceChl="
				+ priceChl + ", priceDiscount=" + priceDiscount + ", lingshoujia=" + lingshoujia + ", flbid=" + flbid
				+ ", leibie2=" + leibie2 + ", xlzb=" + xlzb + ", sellPoint1=" + sellPoint1 + ", sellPoint2="
				+ sellPoint2 + ", zizhi=" + zizhi + ", isBtpz=" + isBtpz + ", isZdpz=" + isZdpz + ", isGspMaterial="
				+ isGspMaterial + ", goodsExtend=" + goodsExtend + ", spBal=" + spBal + ", spkfklack=" + spkfklack
				+ ", num=" + num + ", minPicture=" + minPicture + ", pictureList=" + pictureList
				+ "]";
	}

	@Override
	public boolean equals(Object obj) {
		Goods goods = (Goods)obj;
		return this.getSpid().equals(goods.getSpid());
	}

	@Override
	public int hashCode() {
		return spid.hashCode();
	}
}
